package com.cardosystems.infra;

import org.springframework.ui.context.Theme;

import systemobject.terminal.Cli;

import com.aqua.sysobj.conn.CliCommand;
import com.aqua.sysobj.conn.CmdConnection;
import com.aqua.sysobj.conn.WindowsDefaultCliConnection;

import jsystem.extensions.analyzers.text.FindText;
import jsystem.framework.system.SystemObjectImpl;
import jsystem.utils.FileUtils;
import jsystem.utils.exec.Command;
import jsystem.utils.exec.Execute;

public class CardoWindowsStation extends SystemObjectImpl {

	public CmdConnection cli;
	String psCliFolder = "c:\\\"Program Files (x86)\\CSR\\BlueSuite 2.5.0\"\\";
	String cardoUpdaterPath = "C:\\\"Program Files (x86)\\Cardo Updater\"\\CardoUpdater.exe";
	String PsKeyFolder = "C:\\dev\\PsKey\\";
	@Override
	public void init() throws Exception {
		super.init();
		cli = new CmdConnection();
		cli.setCloneOnEveryOperation(true);
	}

	public void disableUSBDevice() throws Exception {
		CliCommand cmd = new CliCommand("devcon disable USB\\VID_2685*");
		cli.handleCliCommand("disabling USB", cmd);
		cli.analyze(new FindText("1 device(s) disabled."));
	}
	
	public void enableUSBDevice() throws Exception {
		CliCommand cmd = new CliCommand("devcon enable USB\\VID_2685*");
		cli.handleCliCommand("enable USB", cmd);
		cli.analyze(new FindText("1 device(s) enabled."));
	}
	
	private void writePsqFile() throws Exception{
		FileUtils.write("dump.psq", "?028c=\r\n?028e=\r\n?029A=\r\n?029C=\r\n?02A0=\r\n?02A2=\r\n?02A8=\r\n?02AF=");
	}

	public String queryPsKeys() throws Exception {
		writePsqFile();
		CliCommand cmd = new CliCommand(
				psCliFolder+"pscli.exe -trans \"CSRTRANS=USB HCIPORT=csr0\" query dump.psr dump.psq");
		cli.handleCliCommand("", cmd);
		cli.analyze(new FindText("concluded with Success"));
		cmd.setCommand("type dump.psr");
		cli.handleCliCommand("Querying PsKeys", cmd);
		setTestAgainstObject(cli.getTestAgainstObject());
		return cli.getTestAgainstObject().toString();
	}
	
	public void closeCardoUpdater() throws Exception{
		CliCommand cmd = new CliCommand("taskkill /F /IM cardoupdater.exe");
		cli.handleCliCommand("Closing Cardo Updater", cmd);
		cli.analyze(new FindText("has been terminated."),true,false,true);
	}
	
	public void openCardoUpdater() throws Exception{
		CliCommand cmd = new CliCommand(cardoUpdaterPath);
		cli.handleCliCommand("Opening Cardo Updater", cmd);
	}
	
	public void runPsKey35() throws Exception{
		CliCommand cmd = new CliCommand(PsKeyFolder+"Set_PSKey35.exe");
		cli.handleCliCommand("Open PSKey35.exe", cmd);
		Thread.sleep(10000);
		cmd.setCommand("taskkill /F /IM Set_PSKey35.exe");
		cli.handleCliCommand("Close PSKey35.exe", cmd);
	}
	
	public void getRiderDumpFilesFromUUT() throws Exception{
		
		CliCommand cmd = new CliCommand(PsKeyFolder+"usb_host.exe -r -o 0x1F0012 -l 0x19  -f Riders.nam");
		cli.handleCliCommand("", cmd);
		cmd = new CliCommand(PsKeyFolder+"usb_host.exe -r -o 0x1F00F3 -l 0x96  -f Riders.phn");
		cli.handleCliCommand("", cmd);
		cmd = new CliCommand(PsKeyFolder+"usb_host.exe -r -o 0x1F0639 -l 0x3E8 -f Riders.aud");
		cli.handleCliCommand("", cmd);
	}
	
	

	
}
